package com.hetwen.search.face;

import java.io.ByteArrayOutputStream;

import org.json.JSONObject;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.Log;

import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;

public class FaceDetector {

	protected static final String TAG = "FaceDetector";
	
	private DetectCallback callback;
	
	public void setDetectCallback(DetectCallback detectCallback) { 
		callback = detectCallback;
	}

	public void detect(final Bitmap image) {
		
		new Thread(new Runnable() {
			
			public void run() {
				
				HttpRequests httpRequests = new HttpRequests("4480afa9b8b364e30ba03819f3e9eff5", "Pz9VFT8AP3g_Pz8_dz84cRY_bz8_Pz8M", true, false);
	    		Log.v(TAG, "image size : " + image.getWidth() + " " + image.getHeight());
	    		
	    		ByteArrayOutputStream stream = new ByteArrayOutputStream();
	    		float scale = Math.min(1, Math.min(600f / image.getWidth(), 600f / image.getHeight()));
	    		Matrix matrix = new Matrix();
	    		matrix.postScale(scale, scale);

	    		Bitmap imageSmall = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, false);
	    		Log.v(TAG, "imageSmall size : " + imageSmall.getWidth() + " " + imageSmall.getHeight());
	    		
	    		imageSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
	    		byte[] array = stream.toByteArray();
	    		
	    		try {
	    			//detect
	    			Log.i(TAG, "Detecting...");
					JSONObject json = httpRequests.detectionDetect(new PostParameters().setImg(array));
					Log.v(TAG, json.toString());
					
					// use the red paint
					Paint paint = new Paint();
					paint.setColor(Color.RED);
					paint.setStrokeWidth(Math.max(image.getWidth(), image.getHeight()) / 200f);
					float strokeWidth = paint.getStrokeWidth();

					// create a new canvas
					Bitmap bitmap = Bitmap.createBitmap(image.getWidth(), image.getHeight(),
							image.getConfig());
					Canvas canvas = new Canvas(bitmap);
					canvas.drawBitmap(image, new Matrix(), null);
					
					// find out all faces
					final int count = json.getJSONArray("face").length();
					for (int i = 0; i < count; ++i) {
						float x, y, w, h;
						// get the center point
						x = (float) json.getJSONArray("face").getJSONObject(i)
								.getJSONObject("position").getJSONObject("center")
								.getDouble("x");
						y = (float) json.getJSONArray("face").getJSONObject(i)
								.getJSONObject("position").getJSONObject("center")
								.getDouble("y");

						// get face size
						w = (float) json.getJSONArray("face").getJSONObject(i)
								.getJSONObject("position").getDouble("width");
						h = (float) json.getJSONArray("face").getJSONObject(i)
								.getJSONObject("position").getDouble("height");

						// change percent value to the real size
						x = x / 100 * image.getWidth();
						w = w / 100 * image.getWidth() * 0.7f;
						y = y / 100 * image.getHeight();
						h = h / 100 * image.getHeight() * 0.7f;

						// draw the box to mark it out
						//左
						canvas.drawLine(x - w, y - h-(strokeWidth/2), x - w, y + h+(strokeWidth/2), paint);
						//上
						canvas.drawLine(x - w, y - h, x + w, y - h, paint);
						//下
						canvas.drawLine(x + w, y + h, x - w, y + h, paint);
						//右
						canvas.drawLine(x + w, y + h+(strokeWidth/2), x + w, y - h-(strokeWidth/2), paint);
					}
					
					//finished , then call the callback function
					if (callback != null) {
						Log.i(TAG, "call callback");
						callback.detectResult(bitmap);
					}
					
					
					//构造请求参数
					PostParameters params = new PostParameters();
					params.setPersonName("范冰冰");
					
					JSONObject trainVerify = httpRequests.trainVerify(params);
					Log.v(TAG, trainVerify.toString());
					
					//获取face_id
					String face_id = json.getJSONArray("face").getJSONObject(0).getString("face_id");
					Log.i(TAG, "face_id:"+face_id);
					
					params = new PostParameters();
					params.setFaceId(face_id);
					params.setPersonName("范冰冰");
					
					//识别人脸
					JSONObject json2 = httpRequests.recognitionVerify(params);
					Log.v(TAG, json2.toString());
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
	
}
